//
//  RootViewController.m
//  UIWebView
//
//  Created by  江志磊 on 15-2-3.
//  Copyright (c) 2015年  江 志磊. All rights reserved.
//

#import "RootViewController.h"
#import "SVProgressHUD.h"//第三方加载等待提示控件

@interface RootViewController (){
    //能够加载网页/网址的视图,可以看做苹果浏览器的雏形,支持加载网址、支持处理html标签,还能与javascript脚本语言进行交互,底层封装的是WebKit浏览器内核
    UIWebView *_webView;
}

@end

@implementation RootViewController
#define kUrlString @"http://www.baidu.com"

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"浏览器";
    [self.navigationController setToolbarHidden:NO];
    [self loadDataWithUrlString:kUrlString];
    [self createToolBarItems];
    // Do any additional setup after loading the view.
}

- (void)createToolBarItems{
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(goback)];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(goForward)];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshView)];
    UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(loadHtmlString)];
    UIBarButtonItem *item5 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(loadJSString)];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    NSArray *array = [NSArray arrayWithObjects:spaceItem,item1,spaceItem,item2,spaceItem,item3,spaceItem,item4,spaceItem,item5,spaceItem,nil];
    self.toolbarItems = array;
}
//回到上级界面
- (void)goback{
    [_webView goBack];
}
//前往下级界面
- (void)goForward{
    [_webView goForward];
}
//刷新界面
- (void)refreshView{
    [_webView reload];
}

//加载html的页面
- (void)loadHtmlString{
    //获取html页面的路径
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"1_guanlan_4_3" ofType:@"html"];
    //读取页面的字符串数据
    NSString *htmlString = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    //loadHTMLString 加载字符串,baseURL 字符串数据的来源
    [_webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]];
}
//加载简单的javascript脚本语言
- (void)loadJSString{
    //获取网页title标签的值
    NSString *js1 =@"document.title";
    //stringByEvaluatingJavaScriptFromString  webView 处理js语句的方法
    NSString *title=[_webView stringByEvaluatingJavaScriptFromString:js1];
    NSLog(@"title:%@",title);
    //获取UIWebView加载网址的js语句
    NSString *js2 = @"document.location.href";
    NSString *href = [_webView stringByEvaluatingJavaScriptFromString:js2];
    NSLog(@"href:%@",href);
}


//利用UIWebView 根据网址加载网页
- (void)loadDataWithUrlString:(NSString *)urlString{
    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //webView通过请求对象 请求网页数据
    [_webView loadRequest:request];
    _webView.delegate = self;
    [self.view addSubview:_webView];
}


#pragma mark - UIWebViewDelegate
//开始加载网页数据时调用
- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"start load!");
    //能够显示提示文字的加载等待控件
    [SVProgressHUD showInView:self.view status:@"加载中..."];
}
//网页请求加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"finished!");
    //让加载提示控件消失
    [SVProgressHUD dismiss];
}
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"load error!");
    [SVProgressHUD dismiss];
}

//是否允许webView加载request
//request webView即将加载的请求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSLog(@"request:%@",request.URL);
    
    return YES;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
